package com.corticon.dialogs.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.corticon.dialogs.dao.DataValidationDAO;
import com.corticon.dialogs.model.DataValidation;
import com.corticon.dialogs.util.Pager;

@Repository("dataValidationDAO")
public class DataValidationHibernateDAO extends GenericHibernateDAO<DataValidation, String>
    implements DataValidationDAO {

    private String countByUserGroup =
        "select count(*) from Datavalidation datavalidation where datavalidation.userGroup.name='ALL' or datavalidation.userGroup.name = :userGroup";
    private String countByUserGroupAll =
        "select count(*) from Datavalidation datavalidation where datavalidation.userGroup.name=" + "'ALL'";
    private String queryByUserGroup =
        "from Datavalidation datavalidation where datavalidation.userGroup.name = :userGroup or datavalidation.userGroup.name=" + "'ALL'";
    private String queryByUserGroupAll =
        "from Datavalidation datavalidation where datavalidation.userGroup.name = 'ALL'";

    @Override
    public long getCountByUserGroup(String userGroup) {
        return (Long) getSession().createQuery(countByUserGroup).setParameter("userGroup",
            userGroup).uniqueResult();
    }

    @Override
    public List<DataValidation> findPageByUserGroup(String userGroup, Pager pager) {
        List<DataValidation> datavalidations = new ArrayList<DataValidation>();

        datavalidations = getSession().createQuery(queryByUserGroup).setParameter("userGroup",
            userGroup).
            setFirstResult(pager.getFirstRecord()).setMaxResults(pager.getPageSize()).list();

        return datavalidations;
    }

    @Override
    public long getCountByUserGroupAll(String userGroup) {
        return (Long) getSession().createQuery(countByUserGroupAll).uniqueResult();
    }

    @Override
    public List<DataValidation> findPageByUserGroupAll(String userGroup, Pager pager) {
        List<DataValidation> datavalidations = new ArrayList<DataValidation>();

        datavalidations = getSession().createQuery(queryByUserGroupAll).
            setFirstResult(pager.getFirstRecord()).setMaxResults(pager.getPageSize()).list();

        return datavalidations;
    }
}

